package com.myproject.thread.daemon;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
//守护线程，只要有一个线程没死，守护线程就不会死
public class DaemonThreadTest1 {

    public static BlockingQueue blockingQueue = new LinkedBlockingQueue<Integer>();


    public static void main(String[] args) {

        blockingQueue.add(3);
        blockingQueue.add(5);
        blockingQueue.add(7);
        blockingQueue.add(9);

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                for ( ; ;) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("demon thread");
                }
            }
        });
        thread.setDaemon(true);
        thread.start();

        for (int i = 0; i < 3; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int count = (int) blockingQueue.poll();
                    System.out.println(Thread.currentThread()+" sum "+count);
                    for (int j = 0; j < count; j++) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " "+j);
                    }
                }
            }).start();
        }
        System.out.println(Thread.currentThread().getName());
    }
}
